<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.2/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"littlefxc.github.io","root":"/","images":"/images","scheme":"Mist","version":"8.2.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}};
  </script>
<meta name="description" content="Docker + mysql主从配置文件结构">
<meta property="og:type" content="article">
<meta property="og:title" content="Docker+mysql主从配置">
<meta property="og:url" content="http://littlefxc.github.io/2019/05/07/Docker-mysql%E4%B8%BB%E4%BB%8E%E9%85%8D%E7%BD%AE/index.html">
<meta property="og:site_name" content="一年春又来">
<meta property="og:description" content="Docker + mysql主从配置文件结构">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20190423172956747.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xpdHRsZV9meGM=,size_16,color_FFFFFF,t_70">
<meta property="article:published_time" content="2019-05-07T12:28:54.000Z">
<meta property="article:modified_time" content="2021-03-25T13:15:48.942Z">
<meta property="article:author" content="一年春又来">
<meta property="article:tag" content="docker">
<meta property="article:tag" content="mysql">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/20190423172956747.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xpdHRsZV9meGM=,size_16,color_FFFFFF,t_70">


<link rel="canonical" href="http://littlefxc.github.io/2019/05/07/Docker-mysql%E4%B8%BB%E4%BB%8E%E9%85%8D%E7%BD%AE/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>
<title>Docker+mysql主从配置 | 一年春又来</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="一年春又来" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">一年春又来</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="home                          //首页 fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="archive          //归档 fa-fw"></i>归档</a></li>
        <li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="th           //分类 fa-fw"></i>分类</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="tags                     //标签 fa-fw"></i>标签</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Docker-mysql%E4%B8%BB%E4%BB%8E%E9%85%8D%E7%BD%AE"><span class="nav-text">Docker + mysql主从配置</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%96%87%E4%BB%B6%E7%BB%93%E6%9E%84"><span class="nav-text">文件结构</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%BB-mysql-%E7%9A%84%E9%85%8D%E7%BD%AE-master-cnf"><span class="nav-text">主 mysql 的配置:master.cnf</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BB%8E-mysql-%E7%9A%84%E9%85%8D%E7%BD%AE-slave-cnf"><span class="nav-text">从 mysql 的配置:slave.cnf</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%85%8D%E7%BD%AEmysql%E4%B8%BB%E4%BB%8E%E7%9A%84%E8%84%9A%E6%9C%AC%EF%BC%9Amysql-connector-sh"><span class="nav-text">配置mysql主从的脚本：mysql_connector.sh</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#docker-compose-%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6"><span class="nav-text">docker-compose 配置文件</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">一年春又来</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">234</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">38</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">125</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2019/05/07/Docker-mysql%E4%B8%BB%E4%BB%8E%E9%85%8D%E7%BD%AE/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Docker+mysql主从配置
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2019-05-07 20:28:54" itemprop="dateCreated datePublished" datetime="2019-05-07T20:28:54+08:00">2019-05-07</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-03-25 21:15:48" itemprop="dateModified" datetime="2021-03-25T21:15:48+08:00">2021-03-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Java/" itemprop="url" rel="index"><span itemprop="name">Java</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="Docker-mysql主从配置"><a href="#Docker-mysql主从配置" class="headerlink" title="Docker + mysql主从配置"></a>Docker + mysql主从配置</h1><h2 id="文件结构"><a href="#文件结构" class="headerlink" title="文件结构"></a>文件结构</h2><p><img src="https://img-blog.csdnimg.cn/20190423172956747.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xpdHRsZV9meGM=,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<span id="more"></span>

<h2 id="主-mysql-的配置-master-cnf"><a href="#主-mysql-的配置-master-cnf" class="headerlink" title="主 mysql 的配置:master.cnf"></a>主 mysql 的配置:master.cnf</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">[mysqld]</span><br><span class="line">## 设置server_id，一般设置为IP，注意要唯一</span><br><span class="line">server_id&#x3D;100</span><br><span class="line">## 复制过滤：也就是指定哪个数据库不用同步（mysql库一般不同步）</span><br><span class="line">binlog-ignore-db&#x3D;mysql</span><br><span class="line">## 开启二进制日志功能，可以随便取，最好有含义（关键就是这里了）</span><br><span class="line">log-bin&#x3D;replicas-mysql-bin</span><br><span class="line">## 为每个session分配的内存，在事务过程中用来存储二进制日志的缓存</span><br><span class="line">binlog_cache_size&#x3D;1M</span><br><span class="line">## 主从复制的格式（mixed,statement,row，默认格式是statement）</span><br><span class="line">binlog_format&#x3D;mixed</span><br><span class="line">## 二进制日志自动删除&#x2F;过期的天数。默认值为0，表示不自动删除。</span><br><span class="line">expire_logs_days&#x3D;7</span><br><span class="line">## 跳过主从复制中遇到的所有错误或指定类型的错误，避免slave端复制中断。</span><br><span class="line">## 如：1062错误是指一些主键重复，1032错误是因为主从数据库数据不一致</span><br><span class="line">slave_skip_errors&#x3D;1062</span><br></pre></td></tr></table></figure>

<h2 id="从-mysql-的配置-slave-cnf"><a href="#从-mysql-的配置-slave-cnf" class="headerlink" title="从 mysql 的配置:slave.cnf"></a>从 mysql 的配置:slave.cnf</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">[mysqld]</span><br><span class="line">## 设置server_id，一般设置为IP，注意要唯一</span><br><span class="line">server_id&#x3D;101</span><br><span class="line">## 复制过滤：也就是指定哪个数据库不用同步（mysql库一般不同步）</span><br><span class="line">binlog-ignore-db&#x3D;mysql</span><br><span class="line">## 开启二进制日志功能，以备Slave作为其它Slave的Master时使用</span><br><span class="line">log-bin&#x3D;replicas-mysql-slave1-bin</span><br><span class="line">## 为每个session 分配的内存，在事务过程中用来存储二进制日志的缓存</span><br><span class="line">binlog_cache_size&#x3D;1M</span><br><span class="line">## 主从复制的格式（mixed,statement,row，默认格式是statement）</span><br><span class="line">binlog_format&#x3D;mixed</span><br><span class="line">## 二进制日志自动删除&#x2F;过期的天数。默认值为0，表示不自动删除。</span><br><span class="line">expire_logs_days&#x3D;7</span><br><span class="line">## 跳过主从复制中遇到的所有错误或指定类型的错误，避免slave端复制中断。</span><br><span class="line">## 如：1062错误是指一些主键重复，1032错误是因为主从数据库数据不一致</span><br><span class="line">slave_skip_errors&#x3D;1062</span><br><span class="line">## relay_log配置中继日志</span><br><span class="line">relay_log&#x3D;replicas-mysql-relay-bin</span><br><span class="line">## log_slave_updates表示slave将复制事件写进自己的二进制日志</span><br><span class="line">log_slave_updates&#x3D;1</span><br><span class="line">## 防止改变数据(除了特殊的线程)</span><br><span class="line">read_only&#x3D;1</span><br></pre></td></tr></table></figure>

<h2 id="配置mysql主从的脚本：mysql-connector-sh"><a href="#配置mysql主从的脚本：mysql-connector-sh" class="headerlink" title="配置mysql主从的脚本：mysql_connector.sh"></a>配置mysql主从的脚本：mysql_connector.sh</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line">BASE_PATH=$(dirname <span class="variable">$0</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;Waiting for mysql to get up&quot;</span></span><br><span class="line"><span class="comment"># Give 60 seconds for master and slave to come up</span></span><br><span class="line">sleep 60</span><br><span class="line"></span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;Create MySQL Servers (master / slave repl)&quot;</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;-----------------&quot;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;* Create replication user&quot;</span></span><br><span class="line"></span><br><span class="line">mysql --host mysqlslave -uroot -p<span class="variable">$MYSQL_SLAVE_PASSWORD</span> -AN -e <span class="string">&#x27;STOP SLAVE;&#x27;</span>;</span><br><span class="line">mysql --host mysqlmaster -uroot -p<span class="variable">$MYSQL_MASTER_PASSWORD</span> -AN -e <span class="string">&#x27;RESET SLAVE ALL;&#x27;</span>;</span><br><span class="line"></span><br><span class="line">mysql --host mysqlmaster -uroot -p<span class="variable">$MYSQL_MASTER_PASSWORD</span> -AN -e <span class="string">&quot;CREATE USER &#x27;<span class="variable">$MYSQL_REPLICATION_USER</span>&#x27;@&#x27;%&#x27;;&quot;</span></span><br><span class="line">mysql --host mysqlmaster -uroot -p<span class="variable">$MYSQL_MASTER_PASSWORD</span> -AN -e <span class="string">&quot;GRANT REPLICATION SLAVE ON *.* TO &#x27;<span class="variable">$MYSQL_REPLICATION_USER</span>&#x27;@&#x27;%&#x27; IDENTIFIED BY &#x27;<span class="variable">$MYSQL_REPLICATION_PASSWORD</span>&#x27;;&quot;</span></span><br><span class="line">mysql --host mysqlmaster -uroot -p<span class="variable">$MYSQL_MASTER_PASSWORD</span> -AN -e <span class="string">&#x27;flush privileges;&#x27;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;* Set MySQL01 as master on MySQL02&quot;</span></span><br><span class="line"></span><br><span class="line">MYSQL01_Position=$(<span class="built_in">eval</span> <span class="string">&quot;mysql --host mysqlmaster -uroot -p<span class="variable">$MYSQL_MASTER_PASSWORD</span> -e &#x27;show master status \G&#x27; | grep Position | sed -n -e &#x27;s/^.*: //p&#x27;&quot;</span>)</span><br><span class="line">MYSQL01_File=$(<span class="built_in">eval</span> <span class="string">&quot;mysql --host mysqlmaster -uroot -p<span class="variable">$MYSQL_MASTER_PASSWORD</span> -e &#x27;show master status \G&#x27;     | grep File     | sed -n -e &#x27;s/^.*: //p&#x27;&quot;</span>)</span><br><span class="line">MASTER_IP=$(<span class="built_in">eval</span> <span class="string">&quot;getent hosts mysqlmaster|awk &#x27;&#123;print \$1&#125;&#x27;&quot;</span>)</span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$MASTER_IP</span></span><br><span class="line">mysql --host mysqlslave -uroot -p<span class="variable">$MYSQL_SLAVE_PASSWORD</span> -AN -e <span class="string">&quot;CHANGE MASTER TO master_host=&#x27;mysqlmaster&#x27;, master_port=3306, \</span></span><br><span class="line"><span class="string">        master_user=&#x27;<span class="variable">$MYSQL_REPLICATION_USER</span>&#x27;, master_password=&#x27;<span class="variable">$MYSQL_REPLICATION_PASSWORD</span>&#x27;, master_log_file=&#x27;<span class="variable">$MYSQL01_File</span>&#x27;, \</span></span><br><span class="line"><span class="string">        master_log_pos=<span class="variable">$MYSQL01_Position</span>;&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;* Set MySQL02 as master on MySQL01&quot;</span></span><br><span class="line"></span><br><span class="line">MYSQL02_Position=$(<span class="built_in">eval</span> <span class="string">&quot;mysql --host mysqlslave -uroot -p<span class="variable">$MYSQL_SLAVE_PASSWORD</span> -e &#x27;show master status \G&#x27; | grep Position | sed -n -e &#x27;s/^.*: //p&#x27;&quot;</span>)</span><br><span class="line">MYSQL02_File=$(<span class="built_in">eval</span> <span class="string">&quot;mysql --host mysqlslave -uroot -p<span class="variable">$MYSQL_SLAVE_PASSWORD</span> -e &#x27;show master status \G&#x27;     | grep File     | sed -n -e &#x27;s/^.*: //p&#x27;&quot;</span>)</span><br><span class="line"></span><br><span class="line">SLAVE_IP=$(<span class="built_in">eval</span> <span class="string">&quot;getent hosts mysqlslave|awk &#x27;&#123;print \$1&#125;&#x27;&quot;</span>)</span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$SLAVE_IP</span></span><br><span class="line">mysql --host mysqlmaster -uroot -p<span class="variable">$MYSQL_MASTER_PASSWORD</span> -AN -e <span class="string">&quot;CHANGE MASTER TO master_host=&#x27;mysqlslave&#x27;, master_port=3306, \</span></span><br><span class="line"><span class="string">        master_user=&#x27;<span class="variable">$MYSQL_REPLICATION_USER</span>&#x27;, master_password=&#x27;<span class="variable">$MYSQL_REPLICATION_PASSWORD</span>&#x27;, master_log_file=&#x27;<span class="variable">$MYSQL02_File</span>&#x27;, \</span></span><br><span class="line"><span class="string">        master_log_pos=<span class="variable">$MYSQL02_Position</span>;&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;* Start Slave on both Servers&quot;</span></span><br><span class="line">mysql --host mysqlslave -uroot -p<span class="variable">$MYSQL_SLAVE_PASSWORD</span> -AN -e <span class="string">&quot;start slave;&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;Increase the max_connections to 2000&quot;</span></span><br><span class="line">mysql --host mysqlmaster -uroot -p<span class="variable">$MYSQL_MASTER_PASSWORD</span> -AN -e <span class="string">&#x27;set GLOBAL max_connections=2000&#x27;</span>;</span><br><span class="line">mysql --host mysqlslave -uroot -p<span class="variable">$MYSQL_SLAVE_PASSWORD</span> -AN -e <span class="string">&#x27;set GLOBAL max_connections=2000&#x27;</span>;</span><br><span class="line"></span><br><span class="line">mysql --host mysqlslave -uroot -p<span class="variable">$MYSQL_SLAVE_PASSWORD</span> -e <span class="string">&quot;show slave status \G&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;MySQL servers created!&quot;</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;--------------------&quot;</span></span><br><span class="line"><span class="built_in">echo</span></span><br><span class="line"><span class="built_in">echo</span> Variables available fo you :-</span><br><span class="line"><span class="built_in">echo</span></span><br><span class="line"><span class="built_in">echo</span> MYSQL01_IP       : mysqlmaster</span><br><span class="line"><span class="built_in">echo</span> MYSQL02_IP       : mysqlslave</span><br></pre></td></tr></table></figure>

<h2 id="docker-compose-配置文件"><a href="#docker-compose-配置文件" class="headerlink" title="docker-compose 配置文件"></a>docker-compose 配置文件</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">version: &#39;2&#39;</span><br><span class="line">services:</span><br><span class="line">  mysqlmaster:</span><br><span class="line">    image: mysql:5.7.15</span><br><span class="line">    container_name: &quot;mysql_master1&quot;</span><br><span class="line">    environment:</span><br><span class="line">      - &quot;MYSQL_ROOT_PASSWORD&#x3D;root&quot;</span><br><span class="line">    volumes:</span><br><span class="line">      - &quot;.&#x2F;data&#x2F;mysql-master:&#x2F;var&#x2F;lib&#x2F;mysql&#x2F;&quot;</span><br><span class="line">      - &quot;.&#x2F;config&#x2F;mysql-master:&#x2F;etc&#x2F;mysql&#x2F;conf.d&#x2F;&quot;</span><br><span class="line">    ports:</span><br><span class="line">      - &quot;3306:3306&quot;</span><br><span class="line">  mysqlslave:</span><br><span class="line">    image: mysql:5.7.15</span><br><span class="line">    container_name: &quot;mysql_slave1&quot;</span><br><span class="line">    environment:</span><br><span class="line">      - &quot;MYSQL_ROOT_PASSWORD&#x3D;root&quot;</span><br><span class="line">    volumes:</span><br><span class="line">      - &quot;.&#x2F;data&#x2F;mysql-slave:&#x2F;var&#x2F;lib&#x2F;mysql&#x2F;&quot;</span><br><span class="line">      - &quot;.&#x2F;config&#x2F;mysql-slave:&#x2F;etc&#x2F;mysql&#x2F;conf.d&#x2F;&quot;</span><br><span class="line">    ports:</span><br><span class="line">      - &quot;3307:3306&quot;</span><br><span class="line">  mysqlconfigure:</span><br><span class="line">    image: mysql:5.7.15</span><br><span class="line">    container_name: &quot;mysql_configure&quot;</span><br><span class="line">    environment:</span><br><span class="line">      - &quot;MYSQL_SLAVE_PASSWORD&#x3D;root&quot;</span><br><span class="line">      - &quot;MYSQL_MASTER_PASSWORD&#x3D;root&quot;</span><br><span class="line">      - &quot;MYSQL_ROOT_PASSWORD&#x3D;root&quot;</span><br><span class="line">      - &quot;MYSQL_REPLICATION_USER&#x3D;repl&quot;</span><br><span class="line">      - &quot;MYSQL_REPLICATION_PASSWORD&#x3D;repl&quot;</span><br><span class="line">    volumes:</span><br><span class="line">      - &quot;.&#x2F;mysql_connector.sh:&#x2F;tmp&#x2F;mysql_connector.sh&quot;</span><br><span class="line">    command: &#x2F;bin&#x2F;bash -x &#x2F;tmp&#x2F;mysql_connector.sh</span><br></pre></td></tr></table></figure>
    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/docker/" rel="tag"># docker</a>
              <a href="/tags/mysql/" rel="tag"># mysql</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2019/05/07/JedisCommand%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E/" rel="prev" title="JedisCommand接口说明">
                  <i class="fa fa-chevron-left"></i> JedisCommand接口说明
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2019/05/07/Spring-MyBatis-MySQL%E4%B8%BB%E4%BB%8E%E5%88%86%E7%A6%BB/" rel="next" title="Spring+MyBatis+MySQL主从分离">
                  Spring+MyBatis+MySQL主从分离 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>







<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">一年春又来</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/mist/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  
<script src="/js/local-search.js"></script>






  





</body>
</html>
